package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import utils.MyUtil;

public class HJ41_静态代码扫描服务 {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/41.txt"));
    MyUtil.START();
    int M = in.nextInt();
    in.nextLine(); // 吸收回车
    String[] FArray = in.nextLine().split(" ");
    String[] SArray = in.nextLine().split(" ");
    Map<String, Integer> index = new HashMap<>();
    Map<String, Integer> count = new HashMap<>();
    for (int i = 0; i < FArray.length; i++) {
      count.put(FArray[i], count.getOrDefault(FArray[i], 0) + 1);
      if (!index.containsKey(FArray[i])) {
        index.put(FArray[i], i);
      }
    }
    int res = 0;
    // 以下有两种 Map 遍历方式
    for (Map.Entry<String, Integer> entry : count.entrySet()) {
      res +=
          Math.min(
              entry.getValue() * Integer.parseInt(SArray[index.get(entry.getKey())]),
              Integer.parseInt(SArray[index.get(entry.getKey())]) + M);
    }
    //    for (String key : count.keySet()) {
    //      res +=
    //          Math.min(
    //              count.get(key) * Integer.parseInt(SArray[index.get(key)]),
    //              Integer.parseInt(SArray[index.get(key)]) + M);
    //    }
    System.out.println(res);
    MyUtil.END();
  }
}
